﻿Imports System.Windows.Forms

Public Class UserFilterDialog

    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        If DatesCheckBox.Checked = False And AreaCheckBox.Checked = False Then
            NoFilterBox.Checked = True
        End If

        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    
    Private Sub Loadboxes()
        Dim sql As String

        'RegionBox
        sql = "Select '<All>' as Region_Code, '<All>' as Region_Name from global_settings_upgrade UNION ALL " & _
              "Select * from Regions"
        RegionBox.DataSource = LoadDataView(sql)
        RegionBox.DisplayMember = "Region_Name"
        RegionBox.ValueMember = "Region_Code"
        RegionBox.Refresh()

        'LABox
        LoadLABox()

        'ProviderBox
        LoadProvidersBox()

    End Sub

    Private Sub LoadProvidersBox()
        Dim sql As String

        sql = "select '<All>' as RID, 0 as UPIN, '<All>' as 'Provider Name', '<All>' as Type From global_settings_upgrade UNION ALL " & _
                    "select rel.RID, p.UPIN, p.Provider_Name as 'Provider Name', pt.Provider_Type_Description as 'Type' " & _
                    "from Local_Authorities la, Providers p, Relationships rel, Provider_Types pt " & _
                    "where la.LA_Code = rel.LA_Code " & _
                    "and p.UPIN = rel.UPIN " & _
                    "and p.Provider_type_Code = pt.Provider_Type_Code "

        If LABox.SelectedValue.ToString <> "<All>" Then
            sql = sql & "and la.LA_Code = '" & LABox.SelectedValue.ToString & "'"
        Else
            If RegionBox.SelectedValue.ToString <> "<All>" Then
                sql = sql & " and la.Region_Code = '" & RegionBox.SelectedValue.ToString & "'"

            End If
        End If

        ProvidersBox.DataSource = LoadDataView(sql)
        ProvidersBox.DisplayMember = "Provider Name"
        ProvidersBox.ValueMember = "RID"
    End Sub

    Private Sub LoadLABox()
        Dim sql As String

        If RegionBox.SelectedValue.ToString = "<All>" Then
            sql = "Select '<All>' as LA_Code, '<All>' as LA_Name, '<All>' as Region_Code from global_settings_upgrade UNION ALL " & _
                  "Select * from Local_Authorities"
        Else
            sql = "Select '<All>' as LA_Code, '<All>' as LA_Name, '<All>' as Region_Code from global_settings_upgrade UNION ALL " & _
                  "Select * from Local_Authorities where Region_Code = '" & RegionBox.SelectedValue.ToString & "'"

        End If

        LABox.DataSource = LoadDataView(sql)
        LABox.DisplayMember = "LA_Name"
        LABox.ValueMember = "LA_Code"
        LABox.Refresh()

    End Sub

    Private Sub UserFilterDialog_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Loadboxes()

        FromBox.Value = Now
        ToBox.Value = Now

        NoFilterBox.Checked = True

        DatesCheckBox.Checked = False
        DateGroup.Enabled = False
        AreaCheckBox.Checked = False
        AreaGroup.Enabled = False
    End Sub

    Private Sub NoFilterBox_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NoFilterBox.CheckedChanged
        If NoFilterBox.Checked = True Then
            DatesCheckBox.Checked = False
            DatesCheckBox.Enabled = False
            DateGroup.Enabled = False
            AreaCheckBox.Checked = False
            AreaCheckBox.Enabled = False
            AreaGroup.Enabled = False
        Else
            DatesCheckBox.Enabled = True
            AreaCheckBox.Enabled = True
        End If
    End Sub

    Private Sub DatesCheckBox_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DatesCheckBox.CheckedChanged
        If DatesCheckBox.Checked = True Then
            DateGroup.Enabled = True
        Else
            DateGroup.Enabled = False
        End If
    End Sub

    Private Sub AreaCheckBox_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AreaCheckBox.CheckedChanged
        If AreaCheckBox.Checked = True Then
            AreaGroup.Enabled = True
        Else
            AreaGroup.Enabled = False
        End If
    End Sub

    Private Sub RegionBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RegionBox.SelectedIndexChanged
        LoadLABox()

    End Sub

    Private Sub LABox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LABox.SelectedIndexChanged
        LoadProvidersBox()

    End Sub

    Private Sub FromBox_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FromBox.ValueChanged
        If ToBox.Value < FromBox.Value Then
            ToBox.Value = FromBox.Value

        End If
    End Sub

    Private Sub ToBox_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToBox.ValueChanged
        If ToBox.Value < FromBox.Value Then
            FromBox.Value = ToBox.Value

        End If
    End Sub
End Class
